home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / daemons / lpd / wait.c < prev   
C/C++ Source or Header  |  1989-08-15  |  5KB  |  184 lines

  1. /* 
  2.  * wait.c --
  3.  *
  4.  *    Procedure to map from Unix wait system call to Sprite.
  5.  *
  6.  * Copyright (C) 1986 Regents of the University of California
  7.  * All rights reserved.
  8.  */
  9.  
  10. #ifndef lint
  11. static char rcsid[] = "$Header: wait.c,v 1.5 88/07/29 18:54:36 ouster Exp $ SPRITE (Berkeley)";
  12. #endif not lint
  13.  
  14. #include <sprite.h>
  15. #include <proc.h>
  16. #include <spriteTime.h>
  17.  
  18. #include "compatInt.h"
  19.  
  20. #include <sys/wait.h>
  21. #include <sys/time.h>
  22. #include <sys/resource.h>
  23.  
  24. char *XXXX;
  25.  
  26.  
  27. /*
  28.  *----------------------------------------------------------------------
  29.  *
  30.  * wait --
  31.  *
  32.  *    Procedure to map from Unix wait system call to Sprite Proc_Wait.
  33.  *
  34.  * Results:
  35.  *    If wait returns due to a stopped or terminated child process,
  36.  *    the process ID of the child is returned to the calling process.
  37.  *    In addition, if statusPtr is non-null then fields in *statusPtr
  38.  *    will be set to contain the exit status of the child whose process
  39.  *    ID is returned.
  40.  *
  41.  *    Otherwise, UNIX_ERROR is returned and errno is set to indicate
  42.  *    the error.
  43.  *
  44.  * Side effects:
  45.  *    None.
  46.  *
  47.  *----------------------------------------------------------------------
  48.  */
  49.  
  50. int
  51. wait(statusPtr)
  52. union wait *statusPtr;
  53. {
  54.     ReturnStatus status;    /* result returned by Proc_Wait */
  55.     int pid;            /* process ID of child */
  56.     int reason;            /* reason child exited */
  57.     int childStatus;        /* returnStatus of child */
  58.     int subStatus;        /* additional signal status */
  59.     int    unixSignal;
  60.  
  61.     status = Proc_Wait(0, (int *) NULL, PROC_WAIT_BLOCK, &pid, &reason,
  62.             &childStatus, &subStatus, (Proc_ResUsage *) NULL);
  63.     if (status != SUCCESS) {
  64.     errno = Compat_MapCode(status);
  65.     return(UNIX_ERROR);
  66.     } else {
  67.     if (statusPtr != NULL)  {
  68.         statusPtr->w_status = 0;
  69.         if (reason == PROC_TERM_SUSPENDED) {
  70.         (void)Compat_SpriteSignalToUnix(childStatus, &unixSignal);
  71.         statusPtr->w_stopval = WSTOPPED;
  72.         statusPtr->w_stopsig = unixSignal;
  73.         } else if (reason == PROC_TERM_SIGNALED ||
  74.                reason == PROC_TERM_RESUMED) {
  75.         (void)Compat_SpriteSignalToUnix(childStatus, &unixSignal);
  76.         statusPtr->w_termsig = unixSignal;
  77.         /* NEED TO HANDLE coredump FIELD */
  78.         } else {
  79.         statusPtr->w_retcode = childStatus;
  80.         }
  81.     }
  82.     return((int) pid);
  83.     }
  84. }
  85.  
  86.  
  87. /*
  88.  *----------------------------------------------------------------------
  89.  *
  90.  * wait3 --
  91.  *
  92.  *    Procedure to map from Unix wait3 system call to Sprite Proc_Wait.
  93.  *
  94.  * Results:
  95.  *    If wait returns due to a stopped or terminated child process,
  96.  *    the process ID of the child is returned to the calling process.
  97.  *    In addition, if statusPtr is non-null then fields in *statusPtr
  98.  *    will be set to contain the exit status of the child whose process
  99.  *    ID is returned.
  100.  *
  101.  *    Otherwise, UNIX_ERROR is returned and errno is set to indicate
  102.  *    the error.
  103.  *
  104.  * Side effects:
  105.  *    None.
  106.  *
  107.  *----------------------------------------------------------------------
  108.  */
  109.  
  110. wait3(statusPtr, options, unixRusagePtr)
  111.     union    wait    *statusPtr;
  112.     int            options;
  113.     struct    rusage    *unixRusagePtr;
  114. {
  115.     Proc_ResUsage spriteRusage;
  116.     ReturnStatus status;    /* result returned by Proc_Wait */
  117.     int pid;            /* process ID of child */
  118.     int reason;            /* reason child exited */
  119.     int childStatus;        /* returnStatus of child */
  120.     int subStatus;        /* additional signal status */
  121.     int    flags;
  122.  
  123. foobar:
  124.     flags = 0;
  125.     if (!(options & WNOHANG)) {
  126.     flags |= PROC_WAIT_BLOCK;
  127.     }
  128.     if (options & WUNTRACED) {
  129.     flags |= PROC_WAIT_FOR_SUSPEND;
  130.     }
  131.  
  132.     status = Proc_Wait(0, (int *) NULL, flags, &pid, &reason, 
  133.         &childStatus, &subStatus, &spriteRusage);
  134.     if (status != SUCCESS) {
  135.     errno = Compat_MapCode(status);
  136.     return(UNIX_ERROR);
  137.     } else {
  138.     if (statusPtr != NULL)  {
  139.         int    unixSignal;
  140.         statusPtr->w_status = 0;
  141.         switch (reason) {
  142.         case PROC_TERM_SUSPENDED: XXXX = "PROC_TERM_SUSPENDED"; break;
  143.         case PROC_TERM_SIGNALED: XXXX = "PROC_TERM_SIGNALED"; break;
  144.         case PROC_TERM_RESUMED: XXXX = "PROC_TERM_RESUMED"; break;
  145.         default: XXXX = "????"; break;
  146.         }
  147.         if (reason == PROC_TERM_SUSPENDED) {
  148.         (void)Compat_SpriteSignalToUnix(childStatus, &unixSignal);
  149.         statusPtr->w_stopval = WSTOPPED;
  150.         statusPtr->w_stopsig = unixSignal;
  151.         } else if (reason == PROC_TERM_RESUMED) {
  152.         goto foobar;
  153.         } else if (reason == PROC_TERM_SIGNALED ||
  154.                reason == PROC_TERM_RESUMED) {
  155.         (void)Compat_SpriteSignalToUnix(childStatus, &unixSignal);
  156.         statusPtr->w_termsig = unixSignal;
  157.         /* NEED TO HANDLE coredump FIELD */
  158.         } else {
  159.         statusPtr->w_retcode = childStatus;
  160.         }
  161.     }
  162.     if (unixRusagePtr != NULL) {
  163.         /*
  164.          * Return the total time used by the process and all its children.
  165.          */
  166.         Time totalKTime;
  167.         Time totalUTime;
  168.  
  169.         bzero((char *) unixRusagePtr, sizeof(*unixRusagePtr));
  170.         Time_Add(spriteRusage.userCpuUsage, spriteRusage.childUserCpuUsage,
  171.                         &totalUTime);
  172.         Time_Add(spriteRusage.kernelCpuUsage,
  173.              spriteRusage.childKernelCpuUsage, &totalKTime);
  174.         unixRusagePtr->ru_utime.tv_sec = totalUTime.seconds;
  175.         unixRusagePtr->ru_utime.tv_usec = totalUTime.microseconds;
  176.         unixRusagePtr->ru_stime.tv_sec = totalKTime.seconds;
  177.         unixRusagePtr->ru_stime.tv_usec = totalKTime.microseconds;
  178.         unixRusagePtr->ru_nvcsw = spriteRusage.numWaitEvents;
  179.         unixRusagePtr->ru_nivcsw = spriteRusage.numQuantumEnds;
  180.     }
  181.     return((int) pid);
  182.     }
  183. }
  184.